3 * Class for handling database updates. Roughly based off of updaters.inc, with
4 * a few improvements :)
9 // Array of updates to perform on the database
10 protected $updates = array();
13 protected $db, $updater;
15 public function __construct( $db ) {
17 switch( $this->db
->getType() ) {
19 $this->updater
= new MysqlUpdater();
22 $this->updater
= new SqliteUpdater();
25 $this->updater
= new OracleUpdater();
28 throw new MWException( __METHOD__
. ' called for unsupported $wgDBtype' );
32 public function doUpdates() {
34 require_once( "$IP/maintenance/updaters.inc" );
36 foreach ( $this->updates
as $version => $updates ) {
37 foreach( $updates as $params ) {
38 $func = array_shift( $params );
39 call_user_func_array( $func, $params );
42 // some updates don't get recorded :(
43 if( $version !== 'always' ) {
44 $this->setAppliedUpdates( $version, $updates );
49 protected function loadUpdates() {
50 // If the updatelog table hasn't been upgraded, we can't use the new
51 // style of recording our steps. Run all to be safe
52 if( !$this->canUseNewUpdatelog() ) {
53 $this->updates
= $this->updater
->getUpdates();
55 foreach( $this->updater
->getUpdates() as $version => $updates ) {
56 $appliedUpdates = $this->getAppliedUpdates( $version );
57 if( !$appliedUpdates ||
$appliedUpdates != $updates ) {
58 $this->updates
[ $version ] = $updates;
62 $this->getOldGlobalUpdates();
65 protected function getAppliedUpdates( $version ) {
66 $key = "updatelist-$version";
67 $val = $this->db
->selectField( 'updatelog', 'ul_value',
68 array( 'ul_key' => $key ), __METHOD__
);
72 return unserialize( $val );
76 protected function setAppliedUpdates( $version, $updates = array() ) {
77 if( !$this->canUseNewUpdatelog() ) {
80 $key = "updatelist-$version";
81 $this->db
->delete( 'updatelog', array( 'ul_key' => $key ), __METHOD__
);
82 $this->db
->insert( 'updatelog',
83 array( 'ul_key' => $key, 'ul_value' => serialize( $updates ) ),
88 * Updatelog was changed in 1.17 to have a ul_value column so we can record
89 * more information about what kind of updates we've done (that's what this
90 * class does). Pre-1.17 wikis won't have this column, and really old wikis
91 * might not even have updatelog at all
95 protected function canUseNewUpdatelog() {
96 return $this->db
->tableExists( 'updatelog' ) &&
97 $this->db
->fieldExists( 'updatelog', 'ul_value' );
101 * Before 1.17, we used to handle updates via stuff like $wgUpdates,
102 * $wgExtNewTables/Fields/Indexes. This is nasty :) We refactored a lot
103 * of this in 1.17 but we want to remain back-compatible for awhile. So
104 * load up these old global-based things into our update list. We can't
105 * version these like we do with our core updates, so they have to go
108 private function getOldGlobalUpdates() {
109 global $wgUpdates, $wgExtNewFields, $wgExtNewTables,
110 $wgExtModifiedFields, $wgExtNewIndexes;
112 if( isset( $wgUpdates[ $this->db
->getType() ] ) ) {
113 foreach( $wgUpdates[ $this->db
->getType() ] as $upd ) {
114 $this->updates
['always'][] = $upd;
118 foreach ( $wgExtNewTables as $tableRecord ) {
119 $this->updates
['always'][] = array(
120 'add_table', $tableRecord[0], $tableRecord[1], true
124 foreach ( $wgExtNewFields as $fieldRecord ) {
125 if ( $fieldRecord[0] != 'user' ||
$doUser ) {
126 $this->updates
['always'][] = array(
127 'add_field', $fieldRecord[0], $fieldRecord[1],
128 $fieldRecord[2], true
133 foreach ( $wgExtNewIndexes as $fieldRecord ) {
134 $this->updates
['always'][] = array(
135 'add_index', $fieldRecord[0], $fieldRecord[1],
136 $fieldRecord[2], true
140 foreach ( $wgExtModifiedFields as $fieldRecord ) {
141 $this->updates
['always'][] = array(
142 'modify_field', $fieldRecord[0], $fieldRecord[1],
143 $fieldRecord[2], true